﻿using System;
//Download by http://www.codefans.net
namespace UpFile
{ /// 
  /// upfile 的摘要说明。
  /// 
    public class upfile
    {
        /// 
        /// 初始化变量
        /// 
        public upfile() { }

        private string mnPath = @"\uploadimages\";
        /// <summary>
        /// 设置上传路径,如:uploadimages\
        /// </summary> 
        public string Path
        {
            get { return mnPath; }
            set { mnPath = @"\" + value + @"\"; }
        }

        private int mnSizes = 200 * 1024;
        /// <summary>
        /// 设置上传文件大小,单位为KB
        /// </summary>
        public int Sizes
        {
            get { return (int)(mnSizes / 1024); }
            set { mnSizes = value * 1024; }
        }

        private string mnFileType = "jpg|gif|bmp|jpeg|png|PNG";
        /// <summary>
        /// 设置上传文件的类型,如:jpg|gif|bmp
        /// </summary>
        public string FileType
        {
            get { return mnFileType; }
            set { mnFileType = value; }
        }

        /// 
        /// 上传图片
        /// 
        /// 上传控件名称
        /// true则以当前时间创建文件夹,false则为设置的文件夹
        /// 返回上传图片的相对路径
        public string fileSaveAs(System.Web.UI.HtmlControls.HtmlInputFile objFile, bool blCreatDirectory)
        {
            try
            {
                string strFilePath = null;
                //以当前时间修改图片的名字或创建文件夹的名字
                string strModifyFileName = DateTime.Now.Year.ToString() + DateTime.Now.Month.ToString() + DateTime.Now.Day.ToString() + DateTime.Now.Hour.ToString() + DateTime.Now.Minute.ToString() + DateTime.Now.Second.ToString() + DateTime.Now.Millisecond.ToString();
                //获得站点的物理路径
                string strUpFilePath = null;
                //如果为true则以当前时间创建文件夹,否则为设置的文件夹
                if (blCreatDirectory)
                {
                    strUpFilePath = System.Web.HttpContext.Current.Server.MapPath("/") + mnPath + @"\" + DateTime.Now.Year.ToString() + DateTime.Now.Month.ToString() + DateTime.Now.Day.ToString() + @"\";
                }
                else
                {
                    strUpFilePath = System.Web.HttpContext.Current.Server.MapPath("/") + mnPath;
                }
                //获得文件的上传的路径
                string strSourcePath = objFile.Value.Trim();
                //判断上传文件是否为空
                if (strSourcePath == "" || strSourcePath == null)
                {
                    message("您没有上传数据呀，是不是搞错了呀!");
                    return null;
                }
                //获得文件扩展名
                string strFileType = strSourcePath.Substring(strSourcePath.LastIndexOf(".") + 1);
                //获得上传文件的大小
                long strLen = objFile.PostedFile.ContentLength;
                //分解允许上传文件的格式
                string[] arrStrTemp = mnFileType.Split('|');
                //设置上传的文件是否是允许的格式
                bool flag = false;
                //判断上传文件大小
                if (strLen >= mnSizes)
                {
                    message("上传的图片不能大于" + mnSizes + "KB");
                    return null;
                }
                //判断上传的文件是否是允许的格式
                foreach (string data in arrStrTemp)
                {
                    if (data == strFileType)
                    {
                        flag = true;
                        break;
                    }
                }
                //如果为真允许上传,为假则不允许上传
                if (!flag)
                {
                    message("目前本系统支持的格式为:" + mnFileType + ",而您的文件格式为：" + strFileType);
                    return null;
                }
                System.IO.DirectoryInfo dir = new System.IO.DirectoryInfo(strUpFilePath);
                //判断文件夹否存在,不存在则创建
                if (!dir.Exists)
                {
                    dir.Create();
                }
                strFilePath = strUpFilePath + strModifyFileName + "." + strFileType;
                objFile.PostedFile.SaveAs(strFilePath);
                if (blCreatDirectory)
                {
                    strFilePath = mnPath + @"\" + DateTime.Now.Year.ToString() + DateTime.Now.Month.ToString() + DateTime.Now.Day.ToString() + @"\" + strModifyFileName + "." + strFileType;
                }
                else
                {
                    strFilePath = mnPath + strModifyFileName + "." + strFileType;

                }

                return strFilePath;
            }
            catch
            {
                //异常
                message("出现未知错误！");
                return null;
            }
        }

        private void message(string msg, string url)
        {
            System.Web.HttpContext.Current.Response.Write(" alert('" + msg + "');window.location='" + url + "' ");
        }

        private void message(string msg)
        {
            System.Web.HttpContext.Current.Response.Write(" alert('" + msg + "'); ");
        }
    }
}



